package com.atguigu.es.demo;

import com.atguigu.es.beans.Emp;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;

import java.io.IOException;

/**
 * Created by Smexy on 2021/7/9
 *
 *     写操作
 *          删除和插入
 *
 *

 */
public class WriteDemo2 {

    public static void main(String[] args) throws IOException {

        //创建一个 JestClientFactory
        JestClientFactory jestClientFactory = new JestClientFactory();

        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        // 从工厂中获取一个 客户端对象
        JestClient jestClient = jestClientFactory.getObject();

        Emp emp = new Emp("1030", "mike", "male", "吃饭", 30, 2222.22);

        ///test/emps/21
        Index index = new Index.Builder(emp)
                .type("emps")
                .index("test")
                .id("23")
                .build();

        DocumentResult execute = jestClient.execute(index);

        System.out.println(execute.getResponseCode());

        System.out.println(execute.getErrorMessage());


        //关闭客户端
        jestClient.close();


    }
}
